﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Persistent;
using S4Store.Domain;
using LX.Core.Query;
using S4Store.Service.Valid.StoreValid;
using S4Store.Domain.SellerKpi;
using S4Store.Service.Valid.SellerKpiValid;
using S4Store.Domain.TelSellerKpi;

namespace S4Store.Service
{
    public class SaleKpiService<TSellerMonthConfig,TSellerKpiMonthConfig,TSellerKpiDayData> : BaseService
        where TSellerMonthConfig : S4Store.Domain.CommonSellerKpi.CommonSellerMonthConfig, new()
        where TSellerKpiMonthConfig : S4Store.Domain.CommonSellerKpi.CommonSellerKpiMonthConfig,new ()
        where TSellerKpiDayData : S4Store.Domain.CommonSellerKpi.CommonSellerKpiDayData, new()
    {
        int comfirmstatus = DataConfirmStatus.Confirmed.ToInt();
        private SellerKpiMonthConfigSave<TSellerKpiMonthConfig> SellerKpiMonthConfigSave = new SellerKpiMonthConfigSave<TSellerKpiMonthConfig>();

        public IQueryable<TSellerKpiMonthConfig> GetSellerKpiMonthConfigQuery()
        {
            return this.GetQuery<TSellerKpiMonthConfig>();
        }
        public IQueryable<TSellerMonthConfig> GetSellerMonthConfigQuery()
        {
            return this.GetQuery<TSellerMonthConfig>();
        }
        public IQueryable<TSellerKpiDayData> GetSellerKpiDayDataQuery()
        {
            return GetTranstion().PersistentContext.Set<TSellerKpiDayData>().Include("Seller").Include("CarBrand").AsQueryable();
            //return this.GetQuery<TSellerKpiDayData>();
        }
        public IQueryable<CarModel> GetCarModelQuery()
        {
            return this.GetQuery<CarModel>();
        }

        public bool IsMonthConfiged(int year, int month, int storeid)
        {
            return this.GetSellerKpiMonthConfigQuery().Any(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
        }

        public TSellerKpiMonthConfig GetSellerKpiMonthConfig(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return this.GetSellerKpiMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
            }
            else
            {
                return this.GetSellerKpiMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            }
        }

        public TSellerMonthConfig GetSellerMonthConfig(int year, int month, int storeid,int sellerid, bool onlycomfirmd = false)
        {
            var query = GetSellerMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month  && t.SellerId == sellerid);
            if (onlycomfirmd)
            {
                query = query.Where(t => t.IsConfirmed);
            }
            var config = query.FirstOrDefault();
            if (config == null)
            {
                config = new TSellerMonthConfig();
                config.SellerId = sellerid;
                config.Month = new DateTime(year, month, 1);
            }
            return config;
        }

        public void SubmitSellerKpiMonthConfig(TSellerKpiMonthConfig config)
        {
            this.Valid(config, SellerKpiMonthConfigSave);
            if (config.Id == 0)
            {
                this.GetSet<TSellerKpiMonthConfig>().Add(config);
            }
            
        }

        public void SubmitSellerMonthConfig(int year, int month,int storeid,List<TSellerMonthConfig> configs)
        {
            var sellerMonthConfigs = GetSellerMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.Seller.StoreId == storeid);
            foreach (var sellerc in sellerMonthConfigs)
            {
                this.GetSet<TSellerMonthConfig>().Remove(sellerc);
            }
            foreach (var config in configs)
            {
                this.GetSet<TSellerMonthConfig>().Add(config);
            }
        }

        public IQueryable<TSellerMonthConfig> GetSellerMonthConfig(int year, int month, int storeid,EmployeeType type, bool onlycomfirmd = false)
        {
            int typevalue = type.ToInt();
            var query = GetSellerMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.Seller.StoreId == storeid && (t.Seller.TypeValue & typevalue) == t.Seller.TypeValue);
            if (onlycomfirmd)
            {
                query = query.Where(t=>t.IsConfirmed);
            }
            return query;
        }

        public IQueryable<TSellerMonthConfig> GetPersonMonthConfig(int year, int month, int sellerid, bool onlycomfirmd = false)
        {
            var query = GetSellerMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.SellerId == sellerid );
            if (onlycomfirmd)
            {
                query = query.Where(t => t.IsConfirmed);
            }
            return query;
        }

        public IQueryable<TSellerKpiDayData> GetSellerKpiDayDatas(int year, int month, int storeid, EmployeeType type, bool onlycomfirmd = false)
        {
            int typevalue = type.ToInt();
            var query = GetSellerKpiDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.Seller.StoreId == storeid&& (t.Seller.TypeValue & typevalue) == t.Seller.TypeValue);
            if (onlycomfirmd)
            {
                query = query.Where(t => t.IsConfirmed);
            }
            return query;
        }

        public IQueryable<TSellerKpiDayData> GetPersonKpiDayDatas(int year, int month, int sellerid, bool onlycomfirmd = false)
        {
            var query = GetSellerKpiDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.SellerId == sellerid);
            if (onlycomfirmd)
            {
                query = query.Where(t => t.IsConfirmed);
            }
            return query;
        }

        public IQueryable<TSellerKpiDayData> GetSellerKpiDayDatas(int year, int month,int day, int storeid)
        {
            return GetSellerKpiDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.Date.Day == day && t.Seller.StoreId == storeid);
        }

        public void SubmitSellerKpiDayDatas(int year, int month, int day, int storeid, List<TSellerKpiDayData> days)
        {
            CarKpiService carkpiservice = new CarKpiService();
            EmployeeService EmployeeService = new EmployeeService();
            this.GetTranstion().Inject(carkpiservice);
            this.GetTranstion().Inject(EmployeeService);

            var carbrandsinplan = carkpiservice.GetCarbrandsForStoreMonth(year, month, storeid).ToList();
            var sellers = EmployeeService.GetEnableEmployee(storeid).ToList();
            var brands = days.GroupBy(o => o.CarBrandId);
            int a = brands.Count();
            if (days.Any(t => carbrandsinplan.All(x => x.Id != t.CarBrandId)))
            {
                throw new Exception("提交的数据里包含未做月初计划的车型");
            }
            if (days.Any(t => sellers.All(x => x.Id != t.SellerId)))
            {
                throw new Exception("提交的数据里包含无效的人员");
            }
            var datas = GetSellerKpiDayDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.Date.Day == day && t.Seller.StoreId == storeid);
            foreach (var data in datas)
            {
                this.GetSet<TSellerKpiDayData>().Remove(data);
            }
            foreach (var config in days)
            {
                this.GetSet<TSellerKpiDayData>().Add(config);
            }
        }
    }

    public class SellerKpiService : SaleKpiService<SellerMonthConfig,SellerKpiMonthConfig,SellerKpiDayData>
    {

    }

    public class TelSellerKpiService : SaleKpiService<TelSellerMonthConfig, TelSellerKpiMonthConfig, TelSellerKpiDayData>
    {

    }

    public class AllSellerKpiService : SaleKpiService<S4Store.Domain.CommonSellerKpi.CommonSellerMonthConfig, S4Store.Domain.CommonSellerKpi.CommonSellerKpiMonthConfig, S4Store.Domain.CommonSellerKpi.CommonSellerKpiDayData>
    {

    }
}
